﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project.MVC.DataAccess
{
    public class ProductAccess
    {
        Linq2SQLDataContext dataContext;
        public ProductAccess()
        {
            dataContext = new Linq2SQLDataContext();
        }
        public IQueryable<Product> GetList()
        {
            return dataContext.Products.OrderByDescending(x => x.Id);
        }
        public void Edit(Product _object)
        {
            var objectDB = dataContext.Products.Where(c => c.Id == _object.Id).SingleOrDefault();
            if (objectDB != null)
            {
                objectDB.Name = _object.Name;
                objectDB.ShortDescription = _object.ShortDescription;
                objectDB.FullDescription = _object.FullDescription;
                objectDB.UpdatedOnUtc = DateTime.Now;
                objectDB.CategoryId = _object.CategoryId;
            }
            dataContext.SubmitChanges();
        }
        public Product GetByKey(int id)
        {
            return dataContext.Products.Where(c => c.Id == id).SingleOrDefault();
        }
        public void Insert(Product _object)
        {
            dataContext.Products.InsertOnSubmit(_object);
            dataContext.SubmitChanges();
        }
        public void Delete(Product _object)
        {
            var objectDB = dataContext.Products.Where(c => c.Id == _object.Id).SingleOrDefault();
            if (objectDB != null)
            {
                objectDB.Deleted = true;
                dataContext.SubmitChanges();
            }
        }
    }
}
